package com.jgssp.ad.sdk.mediation.adapter;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.mediation.MediationConstant;
import com.bytedance.sdk.openadsdk.mediation.ad.MediationViewBinder;
import com.bytedance.sdk.openadsdk.mediation.bridge.custom.native_ad.MediationCustomNativeAd;

import java.util.List;

import cn.jiguang.jgssp.ad.data.ADJgNativeAdInfo;
import cn.jiguang.jgssp.ad.data.ADJgNativeFeedAdInfo;
import cn.jiguang.jgssp.ad.error.ADJgError;
import cn.jiguang.jgssp.ad.listener.ADJgNativeVideoListener;

public class GmNativeAd extends MediationCustomNativeAd {

    private ADJgNativeFeedAdInfo nativeAdInfo;
    private int biddingType;

    public ADJgNativeFeedAdInfo getNativeAdInfo() {
        return nativeAdInfo;
    }

    public GmNativeAd(ADJgNativeFeedAdInfo nativeAdInfo, int biddingType) {
        this.nativeAdInfo = nativeAdInfo;
        this.biddingType = biddingType;
        setExpressAd(false);
        nativeAdInfo.setVideoListener(new ADJgNativeVideoListener() {
            @Override
            public void onVideoStart(ADJgNativeAdInfo adJgNativeAdInfo) {
                callVideoStart();
            }

            @Override
            public void onVideoComplete(ADJgNativeAdInfo adJgNativeAdInfo) {
                callVideoCompleted();
            }

            @Override
            public void onVideoPause(ADJgNativeAdInfo ijUnionNativeVideoAd) {
                callVideoPause();
            }

            @Override
            public void onVideoError(ADJgNativeAdInfo adJgNativeAdInfo, ADJgError adJgError) {
                callVideoError(-1, "视频播放失败");
            }

            @Override
            public void onVideoLoad(ADJgNativeAdInfo adJgNativeAdInfo) {
            }
        });

        if (nativeAdInfo != null) {
            setTitle(nativeAdInfo.getTitle());
            setDescription(nativeAdInfo.getDesc());
            setActionText("查看详情");
            setIconUrl(nativeAdInfo.getImageUrl());
            setImageUrl(nativeAdInfo.getImageUrl());
            setImageList(nativeAdInfo.getImageUrlList());
            setSource(nativeAdInfo.getPlatform());

            if (nativeAdInfo.isVideo()) {
                setAdImageMode(TTAdConstant.IMAGE_MODE_VIDEO);
            } else if (nativeAdInfo.getImageUrlList() != null) {
                if (nativeAdInfo.getImageUrlList().size() == 1) {
                    setAdImageMode(TTAdConstant.IMAGE_MODE_LARGE_IMG);
                } else if (nativeAdInfo.getImageUrlList().size() == 2) {
                    setAdImageMode(TTAdConstant.IMAGE_MODE_SMALL_IMG);
                } else if (nativeAdInfo.getImageUrlList().size() == 3) {
                    setAdImageMode(TTAdConstant.IMAGE_MODE_GROUP_IMG);
                }
            } else {
                setAdImageMode(TTAdConstant.IMAGE_MODE_LARGE_IMG);
            }
        }

    }



    @Override
    public void registerView(Activity activity, final ViewGroup container, final List<View> clickViews, List<View> creativeViews, List<View> directDownloadViews, final MediationViewBinder viewBinder) {
        super.registerView(activity, container, clickViews, creativeViews, directDownloadViews, viewBinder);
        if (getNativeAdInfo().isVideo()) {
            View mediaView = nativeAdInfo.getMediaView(container);
            FrameLayout frameLayout = activity.findViewById(viewBinder.getMediaViewId());
            frameLayout.addView(mediaView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        }
        nativeAdInfo.registerViewForInteraction(container, clickViews.toArray(new View[clickViews.size()]));
    }

    public MediationConstant.AdIsReadyStatus isReadyCondition() {
        if (nativeAdInfo != null && !nativeAdInfo.isReleased()) {
            return MediationConstant.AdIsReadyStatus.AD_IS_READY;
        } else {
            return MediationConstant.AdIsReadyStatus.AD_IS_NOT_READY;
        }
    }

    /**
     * 是否clientBidding广告
     *
     * @return
     */
    public boolean isClientBidding() {
        return biddingType == MediationConstant.AD_TYPE_CLIENT_BIDING;
    }
}
